//package com.camunda.workflow.camunda;
//
//import com.camunda.workflow.camunda.query.CustomGroupQueryImpl;
//import com.camunda.workflow.camunda.query.CustomTenantQueryImpl;
//import com.camunda.workflow.camunda.query.CustomUserQueryImpl;
//import com.camunda.workflow.domain.MapStructMapper;
//import com.camunda.workflow.domain.entity.*;
//import com.camunda.workflow.mapper.*;
//import com.camunda.workflow.utils.EncryUtil;
//import com.mybatisflex.core.query.QueryWrapper;
//import lombok.AllArgsConstructor;
//import org.camunda.bpm.engine.authorization.Permissions;
//import org.camunda.bpm.engine.authorization.Resources;
//import org.camunda.bpm.engine.identity.*;
//import org.camunda.bpm.engine.impl.NativeUserQueryImpl;
//import org.camunda.bpm.engine.impl.context.Context;
//import org.camunda.bpm.engine.impl.identity.IdentityOperationResult;
//import org.camunda.bpm.engine.impl.identity.db.DbIdentityServiceProvider;
//import org.camunda.bpm.engine.impl.interceptor.CommandContext;
//import org.camunda.bpm.engine.impl.persistence.entity.*;
//import org.camunda.bpm.engine.impl.util.EnsureUtil;
//
//import java.util.List;
//import java.util.concurrent.Callable;
//
///**
// * Camunda 还提供了 ReadOnlyIdentityProvider 和 WritableIdentityProvider 接口，允许你自定义用户和组的读写操作，更灵活地集成业务系统用户管理。
// * DbIdentityServiceProvider
// */
//@AllArgsConstructor
//public class CustomIdentityServiceProvider extends DbIdentityServiceProvider {
//    private SysUserMapper sysUserMapper;
//    private SysRoleMapper sysRoleMapper;
//    private SysTenantMapper sysTenantMapper;
//    private SysUserRoleMapper sysUserRoleMapper;
//    private SysTenantUserRoleMapper sysTenantUserRoleMapper;
//
//    @Override
//    public UserEntity findUserById(String userId) {
//        this.checkAuthorization(Permissions.READ, Resources.USER, userId);
//        SysUser sysUser = sysUserMapper.selectOneById(userId);
//        return MapStructMapper.INSTANCE.toCamundaUser(sysUser);
//    }
//
//    @Override
//    public UserQuery createUserQuery() {
//        return new CustomUserQueryImpl(Context.getProcessEngineConfiguration().getCommandExecutorTxRequired());
//    }
//
//    @Override
//    public UserQuery createUserQuery(CommandContext commandContext) {
//        return new CustomUserQueryImpl();
//    }
//
//    @Override
//    public NativeUserQuery createNativeUserQuery() {
//        return new NativeUserQueryImpl(Context.getProcessEngineConfiguration().getCommandExecutorTxRequired());
//    }
//
//    @Override
//    public boolean checkPassword(String userId, String password) {
//        UserEntity user = this.findUserById(userId);
//        return user != null && password != null && EncryUtil.passwdMatch(password, user.getPassword());
//    }
//
//    @Override
//    public GroupEntity findGroupById(String groupId) {
//        this.checkAuthorization(Permissions.READ, Resources.GROUP, groupId);
//        SysRole sysRole = sysRoleMapper.selectOneById(groupId);
//        return MapStructMapper.INSTANCE.toCamundaGroup(sysRole);
//    }
//    @Override
//    public GroupQuery createGroupQuery() {
//        return new CustomGroupQueryImpl(Context.getProcessEngineConfiguration().getCommandExecutorTxRequired());
//    }
//
//    @Override
//    public GroupQuery createGroupQuery(CommandContext commandContext) {
//        return new CustomGroupQueryImpl();
//    }
//
//    @Override
//    public TenantEntity findTenantById(String tenantId) {
//        this.checkAuthorization(Permissions.READ, Resources.TENANT, tenantId);
//        SysTenant sysTenant = sysTenantMapper.selectOneById(tenantId);
//        return MapStructMapper.INSTANCE.toCamundaTenant(sysTenant);
//    }
//
//    @Override
//    public TenantQuery createTenantQuery() {
//        return new CustomTenantQueryImpl(Context.getProcessEngineConfiguration().getCommandExecutorTxRequired());
//    }
//
//    @Override
//    public TenantQuery createTenantQuery(CommandContext commandContext) {
//        return new CustomTenantQueryImpl();
//    }
//
////=============================================================================
//    @Override
//    public UserEntity createNewUser(String userId) {
//        this.checkAuthorization(Permissions.CREATE, Resources.USER, null);
//        return new UserEntity(userId);
//    }
//
//    @Override
//    public IdentityOperationResult saveUser(User user) {
//        UserEntity userEntity = (UserEntity) user;
//        userEntity.setPassword(EncryUtil.passwdEnc(userEntity.getPassword()));
//        String operation;
//        SysUser sysUser = MapStructMapper.INSTANCE.toSysUser(userEntity);
//        if (userEntity.getRevision() == 0) {
//            operation = "create";
//            this.checkAuthorization(Permissions.CREATE, Resources.USER, null);
//            sysUserMapper.insert(sysUser);
//            this.createDefaultAuthorizations(userEntity);
//        } else {
//            operation = "update";
//            this.checkAuthorization(Permissions.UPDATE, Resources.USER, user.getId());
//            sysUserMapper.update(sysUser);
//        }
//        return new IdentityOperationResult(userEntity, operation);
//    }
//
//    @Override
//    public IdentityOperationResult deleteUser(String userId) {
//        this.checkAuthorization(Permissions.DELETE, Resources.USER, userId);
//        UserEntity user = this.findUserById(userId);
//        if (user != null) {
////            this.deleteMembershipsByUserId(userId);
////            this.deleteTenantMembershipsOfUser(userId);
//            sysUserRoleMapper.deleteByQuery(QueryWrapper.create().where(SysUserRole::getUserId).eq(Long.parseLong(userId)));
//            sysTenantUserRoleMapper.deleteByQuery(QueryWrapper.create().where(SysTenantUserRole::getUserId).eq(Long.parseLong(userId)));
//            this.deleteAuthorizations(Resources.USER, userId);
//            Context.getCommandContext().runWithoutAuthorization((Callable<Void>) () -> {
////                List<Tenant> tenants = createTenantQuery().userMember(userId).list();
//                List<SysTenant> sysTenants = sysTenantMapper.selectTenantsByUserId(userId);
//                List<TenantEntity> tenants = MapStructMapper.INSTANCE.toCamundaTenantList(sysTenants);
//                if (tenants != null && !tenants.isEmpty()) {
//                    for (Tenant tenant : tenants) {
//                        deleteAuthorizationsForUser(Resources.TENANT, tenant.getId(), userId);
//                    }
//                }
//
//                return null;
//            });
////            this.getDbEntityManager().delete(user);
//            sysUserMapper.deleteById(userId);
//            return new IdentityOperationResult(null, "delete");
//        } else {
//            return new IdentityOperationResult(null, "none");
//        }
//    }
//
//    @Override
//    public IdentityOperationResult unlockUser(String userId) {
//        UserEntity user = this.findUserById(userId);
//        return user != null ? this.unlockUser(user) : new IdentityOperationResult(null, "none");
//    }
//
//    @Override
//    public GroupEntity createNewGroup(String groupId) {
//        this.checkAuthorization(Permissions.CREATE, Resources.GROUP, null);
//        return new GroupEntity(groupId);
//    }
//
//    @Override
//    public IdentityOperationResult saveGroup(Group group) {
//        GroupEntity groupEntity = (GroupEntity) group;
//        String operation;
//        SysRole sysRole = MapStructMapper.INSTANCE.toSysRole(groupEntity);
//        if (groupEntity.getRevision() == 0) {
//            operation = "create";
//            this.checkAuthorization(Permissions.CREATE, Resources.GROUP, null);
//            sysRoleMapper.insert(sysRole);
//            this.createDefaultAuthorizations(group);
//        } else {
//            operation = "update";
//            this.checkAuthorization(Permissions.UPDATE, Resources.GROUP, group.getId());
//            sysRoleMapper.update(sysRole);
//        }
//
//        return new IdentityOperationResult(groupEntity, operation);
//    }
//
//    @Override
//    public IdentityOperationResult deleteGroup(String groupId) {
//        this.checkAuthorization(Permissions.DELETE, Resources.GROUP, groupId);
//        GroupEntity group = this.findGroupById(groupId);
//        if (group != null) {
////            this.deleteMembershipsByGroupId(groupId);
////            this.deleteTenantMembershipsOfGroup(groupId);
//            sysUserRoleMapper.deleteByQuery(QueryWrapper.create().where(SysUserRole::getRoleId).eq(Long.parseLong(groupId)));
//            sysTenantUserRoleMapper.deleteByQuery(QueryWrapper.create().where(SysTenantUserRole::getRoleId).eq(Long.parseLong(groupId)));
//            this.deleteAuthorizations(Resources.GROUP, groupId);
//            Context.getCommandContext().runWithoutAuthorization((Callable<Void>) () -> {
////                List<Tenant> tenants = createTenantQuery().groupMember(groupId).list();
//                List<SysTenant> sysTenants = sysTenantMapper.selectTenantsByRoleId(groupId);
//                List<TenantEntity> tenants = MapStructMapper.INSTANCE.toCamundaTenantList(sysTenants);
//                if (tenants != null && !tenants.isEmpty()) {
//                    for (Tenant tenant : tenants) {
//                        deleteAuthorizationsForGroup(Resources.TENANT, tenant.getId(), groupId);
//                    }
//                }
//                return null;
//            });
////            this.getDbEntityManager().delete(group);
//            sysRoleMapper.deleteById(groupId);
//            return new IdentityOperationResult(null, "delete");
//        } else {
//            return new IdentityOperationResult(null, "none");
//        }
//    }
//
//    @Override
//    public TenantEntity createNewTenant(String tenantId) {
//        this.checkAuthorization(Permissions.CREATE, Resources.TENANT, null);
//        return new TenantEntity(tenantId);
//    }
//
//    @Override
//    public IdentityOperationResult saveTenant(Tenant tenant) {
//        TenantEntity tenantEntity = (TenantEntity) tenant;
//        String operation;
//        SysTenant sysTenant = MapStructMapper.INSTANCE.toSysTenant(tenantEntity);
//        if (tenantEntity.getRevision() == 0) {
//            operation = "create";
//            this.checkAuthorization(Permissions.CREATE, Resources.TENANT, null);
//            sysTenantMapper.insert(sysTenant);
//            this.createDefaultAuthorizations(tenant);
//        } else {
//            operation = "update";
//            this.checkAuthorization(Permissions.UPDATE, Resources.TENANT, tenant.getId());
//            sysTenantMapper.update(sysTenant);
//        }
//
//        return new IdentityOperationResult(tenantEntity, operation);
//    }
//
//    @Override
//    public IdentityOperationResult deleteTenant(String tenantId) {
//        this.checkAuthorization(Permissions.DELETE, Resources.TENANT, tenantId);
//        TenantEntity tenant = this.findTenantById(tenantId);
//        if (tenant != null) {
////            this.deleteTenantMembershipsOfTenant(tenantId);
//            sysTenantUserRoleMapper.deleteByQuery(QueryWrapper.create().where(SysTenantUserRole::getTenantId).eq(Long.parseLong(tenantId)));
//            this.deleteAuthorizations(Resources.TENANT, tenantId);
////            this.getDbEntityManager().delete(tenant);
//            sysTenantMapper.deleteById(tenantId);
//            return new IdentityOperationResult(null, "delete");
//        } else {
//            return new IdentityOperationResult(null, "none");
//        }
//    }
//
//    @Override
//    public IdentityOperationResult createMembership(String userId, String groupId) {
//        this.checkAuthorization(Permissions.CREATE, Resources.GROUP_MEMBERSHIP, groupId);
//        UserEntity user = this.findUserById(userId);
//        EnsureUtil.ensureNotNull("No user found with id '" + userId + "'.", "user", user);
//        GroupEntity group = this.findGroupById(groupId);
//        EnsureUtil.ensureNotNull("No group found with id '" + groupId + "'.", "group", group);
//        MembershipEntity membership = new MembershipEntity();
//        membership.setUser(user);
//        membership.setGroup(group);
////        this.getDbEntityManager().insert(membership);
//        SysUserRole sysUserRole = MapStructMapper.INSTANCE.toSysUserRole(membership);
//        sysUserRoleMapper.insert(sysUserRole);
//        this.createDefaultMembershipAuthorizations(userId, groupId);
//        return new IdentityOperationResult(null, "create");
//    }
//
//    @Override
//    public IdentityOperationResult deleteMembership(String userId, String groupId) {
//        this.checkAuthorization(Permissions.DELETE, Resources.GROUP_MEMBERSHIP, groupId);
//        if (this.existsMembership(userId, groupId)) {
//            this.deleteAuthorizations(Resources.GROUP_MEMBERSHIP, groupId);
////            Map<String, Object> parameters = new HashMap<>();
////            parameters.put("userId", userId);
////            parameters.put("groupId", groupId);
////            this.getDbEntityManager().delete(MembershipEntity.class, "deleteMembership", parameters);
//            sysUserRoleMapper.deleteByQuery(QueryWrapper.create().where(SysUserRole::getUserId).eq(userId).and(SysUserRole::getRoleId).eq(groupId));
//            return new IdentityOperationResult(null, "delete");
//        } else {
//            return new IdentityOperationResult(null, "none");
//        }
//    }
//
//    @Override
//    public IdentityOperationResult createTenantUserMembership(String tenantId, String userId) {
//        this.checkAuthorization(Permissions.CREATE, Resources.TENANT_MEMBERSHIP, tenantId);
//        TenantEntity tenant = this.findTenantById(tenantId);
//        UserEntity user = this.findUserById(userId);
//        EnsureUtil.ensureNotNull("No tenant found with id '" + tenantId + "'.", "tenant", tenant);
//        EnsureUtil.ensureNotNull("No user found with id '" + userId + "'.", "user", user);
//        TenantMembershipEntity membership = new TenantMembershipEntity();
//        membership.setTenant(tenant);
//        membership.setUser(user);
////        this.getDbEntityManager().insert(membership);
//        SysTenantUserRole sysTenantUserRole = MapStructMapper.INSTANCE.toSysTenantUserRole(membership);
//        sysTenantUserRoleMapper.insert(sysTenantUserRole);
//        this.createDefaultTenantMembershipAuthorizations(tenant, user);
//        return new IdentityOperationResult(null, "create");
//    }
//
//    @Override
//    public IdentityOperationResult createTenantGroupMembership(String tenantId, String groupId) {
//        this.checkAuthorization(Permissions.CREATE, Resources.TENANT_MEMBERSHIP, tenantId);
//        TenantEntity tenant = this.findTenantById(tenantId);
//        GroupEntity group = this.findGroupById(groupId);
//        EnsureUtil.ensureNotNull("No tenant found with id '" + tenantId + "'.", "tenant", tenant);
//        EnsureUtil.ensureNotNull("No group found with id '" + groupId + "'.", "group", group);
//        TenantMembershipEntity membership = new TenantMembershipEntity();
//        membership.setTenant(tenant);
//        membership.setGroup(group);
////        this.getDbEntityManager().insert(membership);
//        SysTenantUserRole sysTenantUserRole = MapStructMapper.INSTANCE.toSysTenantUserRole(membership);
//        sysTenantUserRoleMapper.insert(sysTenantUserRole);
//        this.createDefaultTenantMembershipAuthorizations(tenant, group);
//        return new IdentityOperationResult(null, "create");
//    }
//
//    @Override
//    public IdentityOperationResult deleteTenantUserMembership(String tenantId, String userId) {
//        this.checkAuthorization(Permissions.DELETE, Resources.TENANT_MEMBERSHIP, tenantId);
//        if (this.existsTenantMembership(tenantId, userId, null)) {
//            this.deleteAuthorizations(Resources.TENANT_MEMBERSHIP, userId);
//            this.deleteAuthorizationsForUser(Resources.TENANT, tenantId, userId);
////            Map<String, Object> parameters = new HashMap<>();
////            parameters.put("tenantId", tenantId);
////            parameters.put("userId", userId);
////            this.getDbEntityManager().delete(TenantMembershipEntity.class, "deleteTenantMembership", parameters);
//            sysTenantUserRoleMapper.deleteByQuery(QueryWrapper.create().where(SysTenantUserRole::getTenant).eq(tenantId).and(SysTenantUserRole::getUser).eq(userId));
//            return new IdentityOperationResult(null, "delete");
//        } else {
//            return new IdentityOperationResult(null, "none");
//        }
//    }
//
//    @Override
//    public IdentityOperationResult deleteTenantGroupMembership(String tenantId, String groupId) {
//        this.checkAuthorization(Permissions.DELETE, Resources.TENANT_MEMBERSHIP, tenantId);
//        if (this.existsTenantMembership(tenantId, null, groupId)) {
//            this.deleteAuthorizations(Resources.TENANT_MEMBERSHIP, groupId);
//            this.deleteAuthorizationsForGroup(Resources.TENANT, tenantId, groupId);
////            Map<String, Object> parameters = new HashMap<>();
////            parameters.put("tenantId", tenantId);
////            parameters.put("groupId", groupId);
////            this.getDbEntityManager().delete(TenantMembershipEntity.class, "deleteTenantMembership", parameters);
//            sysTenantUserRoleMapper.deleteByQuery(QueryWrapper.create().where(SysTenantUserRole::getTenant).eq(tenantId).and(SysTenantUserRole::getRoleId).eq(groupId));
//            return new IdentityOperationResult(null, "delete");
//        } else {
//            return new IdentityOperationResult(null, "none");
//        }
//    }
//}
